<?php
/**
 * @version		$Id: edit.php $
 * @package		Joomla.Administrator
 * @subpackage	com_gencode
 * @copyright	Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

// no direct access
defined('_JEXEC') or die;

JHtml::addIncludePath(JPATH_COMPONENT.'/helpers/html');
JHtml::_('behavior.tooltip');
JHtml::_('behavior.formvalidation');

//collation input field
require_once JPATH_COMPONENT_ADMINISTRATOR . DS . 'models' . DS . 'fields' . DS . 'tablecollation.php';

//get default fields
$defaultFields = TableOptionsHelper::defaultFields();

$defaultFieldsId = $defaultFields['id'];
?>

<style>
<!--
#tbl-fields select, #tbl-fields input[type="text"] {
	width: 100px;
}
-->
</style>

<script type="text/javascript">
	window.addEvent('domready', function(){
		$('btn-add-field').addEvent('click', function(){
			var html = $('tmpl-row').get('html');

			var tableRowObject = new Element('tr', { html: html });
			tableRowObject.inject($('tmpl-row'), 'before');

			return false;
		});

		$('tbl-fields').addEvent('click:relay(button.del-field)', function(){
			this.getParent().getParent().destroy();
		});
	});

	Joomla.submitbutton = function(task)
	{
		if (task == 'gendb.cancel' || document.formvalidator.isValid(document.id('gendb-form'))) {
			Joomla.submitform(task, document.getElementById('gendb-form'));
		}
	}
</script>

<form action="<?php echo JRoute::_('index.php?option=com_gencode&layout=edit&id='.(int) $this->item->id); ?>" method="post" name="adminForm" id="gendb-form" class="form-validate">
	<div class="width-100 fltlft">
		<fieldset class="adminform">
			<legend><?php echo empty($this->item->id) ? JText::_('COM_GENCODE_NEW_GENDB') : JText::sprintf('COM_GENCODE_GENDB_DETAILS', $this->item->id); ?></legend>
			<ul class="adminformlist">
				<li><?php echo $this->form->getLabel('name'); ?>
				<?php echo $this->form->getInput('name'); ?></li>

				<li><?php echo $this->form->getLabel('comment'); ?>
				<?php echo $this->form->getInput('comment'); ?></li>

				<li><?php echo $this->form->getLabel('storage_engine'); ?>
				<?php echo $this->form->getInput('storage_engine'); ?></li>

				<li><?php echo $this->form->getLabel('collation'); ?>
				<?php echo $this->form->getInput('collation'); ?></li>
				
				<li><?php echo $this->form->getLabel('created'); ?>
				<?php echo $this->form->getInput('created'); ?></li>

				<li><?php echo $this->form->getLabel('id'); ?>
				<?php echo $this->form->getInput('id'); ?></li>
				
				<li><?php echo $this->form->getLabel('component_id'); ?>
				<?php echo $this->form->getInput('component_id'); ?></li>
				
				<li>
					<table style="float: left; width: 100%;" id="tbl-fields">
						<tr>
							<th>Field</th>
							<th>Type</th>
							<th>Length/Value</th>
							<th>Default</th>
							<?php /*
							<th>Collation</th>
							*/ ?>
							<th>Attributes</th>
							<th>Null</th>
							<th>Comments</th>
							<th>&nbsp;</th>
						</tr>
						<tr>
							<td>id</td>
							<td><?php echo $defaultFieldsId['type']; ?></td>
							<td><?php echo $defaultFieldsId['length']; ?></td>
							<td>None</td>
							<td><?php echo $defaultFieldsId['collation']; ?></td>
							<td><?php echo $defaultFieldsId['attributes']; ?></td>
							<td>No</td>
							<td><?php echo $defaultFieldsId['comment']; ?></td>
							<td>&nbsp;</td>
						</tr>
						<?php foreach ($this->item->fields as $field): ?>
						<tr>
							<td valign="top"><input type="text" name="field[name][]" value="<?php echo $field->name; ?>" /></td>
							<td valign="top"><?php echo TableOptionsHelper::fieldTypeInput('field[type][]', $field->type); ?></td>
							<td valign="top"><input type="text" name="field[length][]" value="<?php echo $field->length; ?>" /></td>
							<td valign="top">
								<?php 
									$fieldDefaultName = array('field[default_type][]', 'field[default_value][]');
									$fieldDefaultValue = array($field->default_type, $field->default_value);
									
									echo TableOptionsHelper::fieldDefaultInput($fieldDefaultName, $fieldDefaultValue);
								?>
							</td>
							<?php /*
							<td valign="top"><?php echo JFormFieldTableCollation::getInput('field[collation][]', $field->collation); ?></td>
							*/?>
							 <td valign="top"><?php echo TableOptionsHelper::fieldAttributesInput('field[attributes][]', $field->attributes); ?></td>
							<td valign="top"><input type="checkbox" name="field[is_null][]" value="1" <?php if($field->is_null == 1) echo 'checked="checked"'; ?> /></td>
							<td valign="top"><input type="text" name="field[comment][]" value="<?php echo $field->comment; ?>" /></td>
							<td valign="top"><button type="button" class="del-field" style="font-size: 10px;">Remove</button></td>
						</tr>
						<?php endforeach; ?>
						<tr id="tmpl-row" style="display: none;">
							<td valign="top"><input type="text" name="field[name][]" /></td>
							<td valign="top"><?php echo TableOptionsHelper::fieldTypeInput('field[type][]'); ?></td>
							<td valign="top"><input type="text" name="field[length][]" /></td>
							<td valign="top">
								<?php 
									$fieldDefaultName = array('field[default_type][]', 'field[default_value][]');
									$fieldDefaultValue = array('', '');
									
									echo TableOptionsHelper::fieldDefaultInput($fieldDefaultName, $fieldDefaultValue);
								?>
							</td>
							<?php /*
							<td valign="top"><?php echo JFormFieldTableCollation::getInput('field[collation][]'); ?></td>
							*/ ?>
							<td valign="top"><?php echo TableOptionsHelper::fieldAttributesInput('field[attributes][]'); ?></td>
							<td valign="top"><input type="checkbox" name="field[is_null][]" /></td>
							<td valign="top"><input type="text" name="field[comment][]" /></td>
							<td valign="top"><button type="button" class="del-field" style="font-size: 10px;">Remove</button></td>
						</tr>
						<?php foreach ($defaultFields as $defaultFieldKey => $defaultField): ?>
						<?php if($defaultFieldKey == 'id') continue; ?>
						<tr>
							<td><?php echo $defaultFieldKey; ?></td>
							<td><?php echo $defaultField['type']; ?></td>
							<td><?php echo $defaultField['length']; ?></td>
							<td><?php echo ($defaultField['type'] == 'int') ? 0 : ( ($defaultField['type'] == 'datetime') ? '0000-00-00 00:00:00' : null )?></td>
							<?php /*
							<td><?php echo $defaultField['collation']; ?></td>
							*/ ?>
							<td><?php echo $defaultField['attributes']; ?></td>
							<td>null</td>
							<td><?php echo $defaultField['field_index']; ?></td>
							<td><?php echo $defaultField['auto_increment'] == true ? 'Yes' : 'No'; ?></td>
							<td><?php echo $defaultField['comment']; ?></td>
							<td>&nbsp;</td>
						</tr>
						<?php endforeach; ?>
						<tr>
							<td colspan="14" style="padding-top: 10px; border-top: 1px solid #CCC;">
								<button id="btn-add-field">Add new field</button>
							</td>
						</tr>
					</table>
				</li>
			</ul>
			<div class="clr"> </div>

		</fieldset>
	</div>

<div class="clr"></div>
<input type="hidden" name="task" value="" />
<?php echo JHtml::_('form.token'); ?>
</form>
